﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Threading;

namespace FBGASaveData
{
    /*

    public class FBGAUdpClient
    {
        private double[,] _dbl = new double[7, 0x7d0];
        public const int DBLLENGTH = 0x7d0;
        private FileSaveData fsd = new FileSaveData();
        private IPEndPoint ipep = null;
        public const int PACKAGESIZE = 20;
        public int timeout = 0x3e8;

        private int CompressByte(byte[] source, ref byte[] des)
        {
            int num = 2;
            int index = 0;
            bool flag = false;
            if (source.Length != 0x200)
            {
                return 0;
            }
            if (des.Length != 0x200)
            {
                return 0;
            }
            for (int i = 0; i < source.Length; i += 2)
            {
                if ((source[i] > 0) || (source[i + 1] > 0))
                {
                    if (!flag)
                    {
                        des[num++] = (byte)(i / 2);
                        des[index = num++] = 0;
                        flag = true;
                    }
                    des[num++] = source[i];
                    des[num++] = source[i + 1];
                    des[index] = (byte)(des[index] + 1);
                }
                else
                {
                    flag = false;
                }
            }
            if (num > 2)
            {
                des[0] = 0xff;
                des[1] = (byte)((num - 2) / 2);
            }
            return ((num > 2) ? num : 0);
        }

        private int DoubleArrayToByteArray(double[] dSource, ref byte[] b)
        {
            if (dSource.Length < 0x100)
            {
                return 0;
            }
            double[] numArray = new double[0x100];
            double num = dSource.Average() + 1000.0;
            byte[] source = new byte[0x200];
            for (int i = 0; i < 0x100; i++)
            {
                if (dSource[i] >= num)
                {
                    byte[] bytes = BitConverter.GetBytes((ushort)dSource[i]);
                    source[2 * i] = bytes[0];
                    source[(2 * i) + 1] = bytes[1];
                }
            }
            return this.CompressByte(source, ref b);
        }

        public void ReceiveData()
        {
            DateTime now = DateTime.Now;
            while (true)
            {
                try
                {
                    if ((CGlobal.udpFGBAReceive == null) || (this.ipep == null))
                    {
                        CGlobal.udpFGBAReceive = new UdpClient(CGlobal.RecPort);
                        CGlobal.udpFGBAReceive.Client.ReceiveBufferSize = 0xa280;
                        this.ipep = new IPEndPoint(IPAddress.Any, 0x1f40);
                    }
                    if (CGlobal.udpFGBAReceive != null)
                    {
                        byte[] bytes = CGlobal.udpFGBAReceive.Receive(ref this.ipep);
                        if ((bytes.Length % 0x200) == 0)
                        {
                            this.WriteSaveBuffer(bytes, 0x200);
                        }
                    }
                }
                catch (Exception exception)
                {
                    System.IO.File.AppendAllText(Application.StartupPath + @"\Eor.log", string.Format("{0}  {1}:{2}\r\n", DateTime.Now, exception.StackTrace, exception.Message));
                }
            }
        }

        private void WriteSaveBuffer(byte[] bytes, int len)
        {
            try
            {
                byte[] src = (byte[])bytes.Clone();
                CGlobal.AddQueue(CGlobal.S_Buffer, src);
                if (CGlobal.S_Buffer.Count > 100)
                {
                    byte[][] state = CGlobal.S_Buffer.ToArray();
                    CGlobal.S_Buffer.Clear();
                    ThreadPool.QueueUserWorkItem(new WaitCallback(this.fsd.SaveData), state);
                }
            }
            catch (Exception exception)
            {
                System.IO.File.AppendAllText(Application.StartupPath + @"\Eor.log", string.Format("{0}  {1}:{2}\r\n", DateTime.Now, exception.StackTrace, exception.Message));
            }
        }

        public double[,] P_dbl
        {
            get
            {
                return this._dbl;
            }
            set
            {
                Array.Copy(value, this._dbl, (value.Length > 0x7d0) ? 0x7d0 : value.Length);
            }
        }
    }
     * */
}

